<?php

namespace Home\Controller;

use Think\Controller;

class IndexController extends CommonController
{
    /***
     *加载首页页面
     */
    public function index()
    {
        //加载新品
        $this->is_news = $this->product->where("status = 1")->where("is_new = 1")->limit(9)->select();
        //加载热销
        $this->is_hots = $this->product->where("status = 1")->where("is_hot = 1")->limit(2)->select();
        //加载推荐
        $this->is_recommends = $this->product->where("status = 1")->where("is_recommend = 1")->limit(2)->select();
        //加载上架
        $this->is_onsales = $this->product->where("status = 1")->where("is_onsale = 1")->limit(2)->select();
        //加载置顶
        $this->is_tops = $this->product->where("status = 1")->where("is_top = 1")->limit(2)->select();
        //加载广告
        $this->adverts = $this->advert->order('sort_order desc')->limit(3)->select();
        $this->display();
    }

    /***
     *加载全部商品页面
     */
    public function all()
    {
        $this->products = $this->product->where("status = 1")->limit(2)->select();
        $this->display();
    }


    public function all_category()
    {
        $categories = $this->category->where('parent_id = 0')->select();
        foreach ($categories as $k => $v) {
            $children = $this->category->where("parent_id = $v[id]")->select();
            $categories[$k]['children'] = $children;
        }
        return $categories;
    }

    /***
     *加载全部分类页面
     */
    public function category()
    {
        $categories = $this->all_category();
        $this->assign('categories', $categories);
        $this->display();
    }

    /***
     *加载商品详情页面
     */
    public function show()
    {
        $id = I('get.id');
        $product = $this->product->where("id = '$id'")->find();

        $galleries = $this->gallery->where("product_id = '$id'")->select();
        //dump($galleries);exit;

        $this->assign(compact('galleries','product'));
        $this->display();
    }
}